package Com.Ring.TradingMobile;

import Com.Ring.TradingMobile.Lib.RET_CODE;
import Com.Ring.TradingMobile.Lib.Session;
import Com.Ring.TradingMobile.Service.WebService;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import Com.Ring.TradingMobile.Lib.Common;

public class ChangePinActivity extends Activity {
	public EditText txtOldPin;
	public EditText txtNewPin;
	public EditText txtReNewPin;
	public Button btn_Ok;
	public Button btn_clear;
	public Button btnBack;
	private static final int DIALOG_LOADING = 0;
	private static String TAG = "ChangPin_TagLog";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.changepin);
		Log.d(TAG, "onCreate");
		
		//Khoi tao doi tuong.
		txtOldPin = (EditText)findViewById(R.id.txtOldPin);				
		txtNewPin = (EditText)findViewById(R.id.txtNewPin);
		txtReNewPin = (EditText)findViewById(R.id.txtReNewPin);
		btn_Ok = (Button)findViewById(R.id.btn_Ok);
		btn_clear = (Button)findViewById(R.id.btn_Clear);
		btnBack = (Button)findViewById(R.id.btnBack);		
		
		btnBack.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				if(changeAsyncTask!=null) {
					changeAsyncTask.cancel(true);
					changeAsyncTask=null;
				}
				finish();
			}
		});
		
		btn_Ok.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
	
				if(txtOldPin.getText().toString().equals(""))
				{
					Toast.makeText(ChangePinActivity.this, R.string.strAlarm_Pin_Empty, Toast.LENGTH_SHORT).show();
					return;
				}
				
				if(txtNewPin.getText().toString().equals(""))
				{
					Toast.makeText(ChangePinActivity.this, R.string.strAlarm_NewPin_Empty, Toast.LENGTH_SHORT).show();
					return;
				}
				
				if(txtReNewPin.getText().toString().equals(""))
				{
					Toast.makeText(ChangePinActivity.this, R.string.strAlarm_ReNewPin_Empty, Toast.LENGTH_SHORT).show();
					return;
				}
								
				showDialog(DIALOG_LOADING);
				if(changeAsyncTask!=null && changeAsyncTask.isCancelled()){
					changeAsyncTask.cancel(true);
					changeAsyncTask=null;
				}
				changeAsyncTask = (ChangeAsyncTask)new ChangeAsyncTask(ChangePinActivity.this).execute(txtOldPin.getText().toString(),txtNewPin.getText().toString());
				
			}
		});
		
		btn_clear.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
	
				txtOldPin.setText("");
				txtNewPin.setText("");
				txtReNewPin.setFocusable(true);
			}
		});
	}
	
	private class ChangeAsyncTask extends AsyncTask<String, Void, Integer>{
		
		Activity activity;
		public ChangeAsyncTask(Activity act) {
			activity = act;
		}
		
		@Override
		protected Integer doInBackground(String... params) {
			activity.showDialog(DIALOG_LOADING);
			int ChangeRetCode = WebService.ChangePin(params[0], params[1]);
			return ChangeRetCode;
		}

		@Override
		protected void onPostExecute(Integer ChangeRetCode) {
			RET_CODE code = Com.Ring.TradingMobile.Lib.Common.GetRetCode(ChangeRetCode);
			activity.removeDialog(DIALOG_LOADING);
			switch (code) {
			case SUCCESS:
				Toast.makeText(ChangePinActivity.this,getResources().getString(R.string.str_changepin_success) , Toast.LENGTH_SHORT).show();
				break;
			default:				
				Toast.makeText(ChangePinActivity.this,Common.GetRetCodeErrorMsg(ChangeRetCode) , Toast.LENGTH_SHORT).show();
				break;
			}
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
			
		}

		@Override
		protected void onProgressUpdate(Void... values) {

			super.onProgressUpdate(values);
		}
	}
	ChangeAsyncTask changeAsyncTask;
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
			case DIALOG_LOADING : {
				ProgressDialog dialog = new ProgressDialog(this);
				dialog.setMessage("Loading...");
				dialog.setIndeterminate(true);
				dialog.setCancelable(true);
				return dialog;
			}
		}
		return null;
	}	
	
	@Override
	protected void onPause() {
		Log.d(TAG, "onPause");
		super.onPause();
	}
	@Override
	protected void onRestart() {
		Log.d(TAG, "onRestart");
		super.onRestart();
	}
	@Override
	protected void onResume() {
		Log.d(TAG, "onResume");
		if(!Session.IsLogin)
		{
			Intent home = new Intent(getBaseContext(), DangNhapActivity.class);
			startActivity(home);
			finish();
		}
		super.onResume();
	}
	@Override
	protected void onStart() {
		Log.d(TAG, "OnStart");
		super.onStart();
	}
	@Override
	protected void onStop() {
		Log.d(TAG, "OnStop");
		super.onStop();
	}
	@Override
	protected void onDestroy() {
		Log.d(TAG, "onDestroy");
		super.onDestroy();
		if(changeAsyncTask!=null) {
			changeAsyncTask.cancel(true);
			changeAsyncTask = null;
		}
	}	
}
